iziToast.settings({
  timeout: 10000,
  progressBar: false,
  close: false,
  closeOnEscape: true,
  position: 'topCenter',
  transitionIn: 'bounceInDown',
  transitionOut: 'flipOutX',
  displayMode: 'replace',
  layout: '1',
  backgroundColor: '#00000040',
  titleColor: '#efefef',
  messageColor: '#efefef',
  icon: 'Fontawesome',
  iconColor: '#efefef'
})

/* 鼠标样式 */
const body = document.querySelector('body')
const element = document.getElementById('g-pointer-1')
const element2 = document.getElementById('g-pointer-2')
const halfAlementWidth = element.offsetWidth / 2
const halfAlementWidth2 = element2.offsetWidth / 2
const hostname = 'http://152.136.255.44:7702/'
function setPosition(x, y) {
  element2.style.transform = `translate(${x - halfAlementWidth2 + 1}px, ${
    y - halfAlementWidth2 + 1
  }px)`
}

body.addEventListener('mousemove', (e) => {
  window.requestAnimationFrame(function () {
    setPosition(e.clientX, e.clientY)
  })
})

let changemore = false

//加载完成后执行
window.addEventListener('load', function () {
  //载入动画
  $('#loading-box').attr('class', 'loaded')
  $('#bg').css(
    'cssText',
    'transform: scale(1);filter: blur(0px);transition: ease 1.5s;'
  )
  $('.cover').css('cssText', 'opacity: 1;transition: ease 1.5s;')
  $('#section').css(
    'cssText',
    'transform: scale(1) !important;opacity: 1 !important;filter: blur(0px) !important'
  )

  //用户欢迎
  setTimeout(function () {
    ;(now = new Date()), (hour = now.getHours())
    if (hour < 6) {
      var hello = '凌晨好'
    } else if (hour < 9) {
      var hello = '早上好'
    } else if (hour < 12) {
      var hello = '上午好'
    } else if (hour < 14) {
      var hello = '中午好'
    } else if (hour < 17) {
      var hello = '下午好'
    } else if (hour < 19) {
      var hello = '傍晚好'
    } else if (hour < 22) {
      var hello = '晚上好'
    } else {
      var hello = '夜深了'
    }
    iziToast.show({
      timeout: 1500,
      icon: false,
      title: hello,
      message: '欢迎来到我的主页',
      position: 'topCenter',
      progressBar: false
    })

    setTimeout(() => {
      $('#phoneNumber').addClass('skip')
    }, 1700)
  }, 500)

  //延迟加载音乐播放器
  // let element = document.createElement('script')
  // element.src = './js/music.js'
  // document.body.appendChild(element)

  //中文字体缓加载-此处写入字体源文件 （暂时弃用）
  //先行加载简体中文子集，后续补全字集
  //由于压缩过后的中文字体仍旧过大，可转移至对象存储或 CDN 加载
  // const font = new FontFace(
  //     "MiSans",
  //     "url(" + "./font/MiSans-Regular.woff2" + ")"
  // );
  // document.fonts.add(font);

  //移动端去除鼠标样式
  if (Boolean(window.navigator.userAgent.match(/AppWebKit.*Mobile.*/))) {
    $('#g-pointer-2').css('display', 'none')
  }

  window.addEventListener('resize', function () {
    //关闭移动端样式
    if (window.innerWidth >= 600) {
      $('#row').attr('class', 'row')
      $('#menu').html("<i class='fa-solid fa-bars'></i>")
      //移除移动端切换功能区
      $('#rightone').attr('class', 'row rightone')
    }

    if (window.innerWidth <= 990) {
      //移动端隐藏更多页面
      $('#container').attr('class', 'container')
      $('#change').html('步儿缓 踩的泪潸潸')
      $('#change1').html('石阶上 转身零落多少银铃啷啷')

      //移动端隐藏弹窗页面
      $('#box').css('display', 'none')
      $('#row').css('display', 'flex')
      $('#more').css('display', 'flex')
    }
  })

  changeText()
  shijian()
})

//移动端菜单栏切换
let switchmenu = false
$('#switchmenu').on('click', function () {
  switchmenu = !switchmenu
  if (switchmenu) {
    $('#row').attr('class', 'row menus')
    $('#menu').html("<i class='fa-solid fa-xmark'></i>")
  } else {
    $('#row').attr('class', 'row')
    $('#menu').html("<i class='fa-solid fa-bars'></i>")
  }
})

//监听网页宽度
window.addEventListener('load', function () {})
//点击功能卡片

document.querySelectorAll('.linkCard').forEach((ele, key) => {
  ele.addEventListener('click', (e) => {
    let clickedID = e.currentTarget.dataset.linkcard
    // TODO

    if (clickedID == 4) {
      console.log('kkk')
      window.location = hostname + 'giveMeMessage'
    } else if (clickedID == 1) {
      window.location = hostname + 'bloghome'
    } else {
      iziToast.show({
        timeout: 2500,
        icon: false,
        title: '哎呀',
        message: '正在开发中...',
        position: 'center',
        progressBar: false
      })
    }
  })
})

function changeText() {
  const textArr = [
    {
      change: '步儿缓 踩的泪潸潸',
      change1: '石阶上 转身零落多少银铃啷啷'
    },
    {
      change: '我永远在这里',
      change1: '涌着爱面朝沧海'
    },
    {
      change: '莫愁前路无知己',
      change1: '天下谁人不识君'
    },
    {
      change: '为什么我眼睛一睁开',
      change1: '你就不是我的了'
    }
  ]
  let i = 0

  setInterval(() => {
    $('#change').attr('class', 'fadeout')
    $('#change1').attr('class', 'fadeout')
    setTimeout(() => {
      i++
      i = i % textArr.length
      $('#change').html(textArr[i].change)
      $('#change1').html(textArr[i].change1)
      $('#change').attr('class', 'fadein')
      $('#change1').attr('class', 'fadein')
    }, 2000)
  }, 15000)
}

var timer = 0
function shijian() {
  clearTimeout(timer)
  dt = new Date()
  let y = dt.getYear() + 1900
  let mm = dt.getMonth() + 1
  let d = dt.getDate()
  let weekday = [
    '星期日',
    '星期一',
    '星期二',
    '星期三',
    '星期四',
    '星期五',
    '星期六'
  ]
  let day = dt.getDay()
  let h = dt.getHours()
  let m = dt.getMinutes()
  let s = dt.getSeconds()
  if (h < 10) {
    h = '0' + h
  }
  if (m < 10) {
    m = '0' + m
  }
  if (s < 10) {
    s = '0' + s
  }
  $('#time').html(
    y +
      '&nbsp;年&nbsp;' +
      mm +
      '&nbsp;月&nbsp;' +
      d +
      '&nbsp;日&nbsp;' +
      "<span class='weekday'>" +
      weekday[day] +
      '</span><br>' +
      "<span class='time-text'>" +
      h +
      ':' +
      m +
      ':' +
      s +
      '</span>'
  )
  timer = setTimeout(shijian, 1000)
}
$('#phoneNumber').on('click', function () {
  copyValue('15503939825')
    .then((res) => {
      console.log('复制成功')
      // var msg = '已复制手机号,微信同号,联系我吧';
      iziToast.show({
        timeout: 2000,
        icon: false,
        title: '已复制',
        message: '手机号,微信同号,联系我吧',
        position: 'center',
        progressBar: false
      })
    })
    .catch(() => {
      iziToast.show({
        timeout: 2000,
        icon: false,
        title: 'call me',
        message: '15503939825',
        position: 'center',
        progressBar: false
      })
    })
})
$('#resume').on('click', function () {
  console.log('click menus')
})

// 先给要复制的文本或者按钮加上点击事件后，并将要复制的值传过来
async function copyValue(val) {
  if (navigator.clipboard && window.isSecureContext) {
    // navigator clipboard 向剪贴板写文本

    return navigator.clipboard.writeText(val)
  } else {
    // 创建text area
    const textArea = document.createElement('textarea')
    textArea.value = val
    // 使text area不在viewport，同时设置不可见
    document.body.appendChild(textArea)
    textArea.focus()
    textArea.select()

    return new Promise((res, rej) => {
      // 执行复制命令并移除文本框
      document.execCommand('copy') ? res() : rej()
      textArea.remove()
    })
  }
}
